package com.web.biz.grgn.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordSecurityUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.web.common.dataDictionary.DE.DE_C_BRAND_EXTTIME;
import com.web.common.dataDictionary.DE.DE_ID;
import com.web.common.dataDictionary.DE.DE_PASSWORD;
import com.web.common.dataDictionary.PO.TCategoryShopPO;
import com.web.common.dataDictionary.PO.TabUserPO;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.FileDownloadUtiles;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class ChangeGrxxService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(ChangeGrxxService.class);
	public ChangeGrxxService() throws Exception {
		super();
	}


	/**
	 * 显示个人信息页面
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Service(serviceName = "ChangeGrxxService_init")
	public IResData init(IReqData req) throws Exception {

		log.debug("开始进入 ChangeGrxxService_init 方法。");
		String yhm = LoginInfoUtil.getLoginUserName();
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		IResData res = new SwordRes();
		Map userMap =(Map) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
		Map shopMap =(Map) SwordServiceUtils.callLocalService("LoginRegisterService_queryShopxxByYhm", yhm);
		String shopTB = StringUtil.parse2String(shopMap.get("cBrandXtbpath"));
		if (StringUtil.isNotEmpty(shopTB)) {
			res.addListWithDataName("fj", FileDownloadUtiles.getfileDownloadMap(shopTB));
		}
		userMap.putAll(shopMap);
		//List categoryList = CacheUtil.getPullData("DM_YW_CATEGORY");
		List list = dao.queryMapListByKey("queryTcategory", null);
		res.addListWithDataName("categoryTitle", list);
		res.addForm("form_grxx",userMap);
		res.addPage("/pages/web/grxx/grxx.jsp");
		return res;
	}

	 		
	/**
	 * 修改保存个人信息
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "ChangeGrxxService_save")
	public IResData save(IReqData req) throws Exception {

		log.debug("开始进入 ChangeGrxxService_save 方法。");
		IResData res = new SwordRes();
		TabUserPO po = (TabUserPO) req.getForm("form_grxx",TabUserPO.class);
		TCategoryShopPO shoppo = (TCategoryShopPO) req.getForm("form_grxx", TCategoryShopPO.class);
		shoppo.setCBrandExttime(new DE_C_BRAND_EXTTIME(DateFormatUtil.getNowDate()));
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String msg="";
	    try {
	    	int i = dao.updatePO(po);
	    	int j = dao.updatePO(shoppo);
	    	if (i>=1 && j>=1) {
				msg="修改成功！";
			}else{
				msg="修改失败！请重试";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg="修改失败！请重试";
		}
		res.addMessage(msg);
		return res;

	}

	 		

	/**
	 * 跳转修改密码页面
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "ChangeGrxxService_changeMm")
	public IResData changeMm(IReqData req) throws Exception {

		log.debug("开始进入 ChangeGrxxService_changeMm 方法。");
		String yhm = LoginInfoUtil.getLoginUserName();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("yhm", yhm);
		IResData res = new SwordRes();
		res.addForm("form_mm", map);
		res.addPage("/pages/web/grxx/changeMm.jsp");
		return res;

	}
	
	/**
	 * 保存修改密码
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "ChangeGrxxService_saveChangeMm")
	public IResData saveChangeMm(IReqData req) throws Exception {
		
		log.debug("开始进入 ChangeGrxxService_saveChangeMm 方法。");
		IResData res = new SwordRes();
		String msg ="";
		Map<String, String> map =req.getForm("form_mm");
		String yhm = map.get("yhm");
		String oldpwd = map.get("oldpwd");
		String oldpwdMD5 = SwordSecurityUtils.md5(oldpwd.getBytes());
		String newpwd = map.get("newpwd");
		String newpwdMD5 = SwordSecurityUtils.md5(newpwd.getBytes());
		SQLParam param = new SQLParam();
		param.addParam(yhm);
		param.addParam(oldpwdMD5);
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String id = StringUtil.parse2String(dao.queryColumnByKey("ChangeGrxxService_queryIdByYhmPwd", param, 1));
		if (!id.equals("")) {
			TabUserPO po = new TabUserPO();
			po.setId(new DE_ID(id));
			po.setPassword(new DE_PASSWORD(newpwdMD5));
			int i = dao.updatePO(po);
			if (i>=1) {
				msg= "修改成功！";
			}else{
				msg= "修改失败，请重试！";
			}
		}else {
			msg = "原密码错误!请重新输入！";
		}
		res.addMessage(msg);
		return res;
	}

}